//
//  RSMessageViewModel.swift
//  ChatDemo
//
//  Created by 范强伟 on 2024/5/10.
//

import UIKit

class RSMessageViewModel: NSObject {
    
    /// 获取聊天室
    /// - Returns: 所有列表
    class func getAllMessage() -> [RSMessageModel]?{
        let model = RealmManager.shareManager().queryObject(objectClass: RSMessageModel.self, .FChatRealm)
        return model
    }
    
    
    /// 获取聊天室所有聊天记录
    /// - Parameter roomId: 房间id
    /// - Returns: 聊天记录
    class func getAllMessage(_ roomId: String) -> RSMessageModel?{
        guard let model = RealmManager.shareManager().queryObject(objectClass: RSMessageModel.self, filter: "roomId = \"\(roomId)\"", .FChatRealm).first else{ return nil }
        return model
    }
    
    /// 添加新的聊天记录
    /// - Parameters:
    ///   - roomId: 房间id
    ///   - modelContent: 聊天记录
    class func updataNewMessage(_ roomId: String,modelContent: RSMessageContentModel){
        guard let model = RealmManager.shareManager().queryObject(objectClass: RSMessageModel.self, filter: "roomId = \"\(roomId)\"", .FChatRealm).first else{
            let model = RSMessageModel()
            model.roomId = roomId
            model.contentList.append(modelContent)
            RealmManager.shareManager().addObject(object: model, .FChatRealm)
            return  }
        RealmManager.shareManager().doWriteHandler(.FChatRealm) {
            model.contentList.append(modelContent)
        }
    }
    
    
    /// 将数据库聊天记录转化为聊天model
    /// - Parameter model: 数据库model
    /// - Returns: 聊天modl
    class func chatMessageToProtocol(_ model: RSMessageModel?) -> [CLRowItemProtocol]?{
        guard let item = model else { return nil }
        var list = Array<CLRowItemProtocol>()
        for item in item.contentList {
            let typeValue = MessageBodyType(rawValue: item.type)
            switch typeValue {
            case .Text:
                let leftItem = CLChatTextItem()
                leftItem.messageId = item.messageId
                leftItem.isFromMyself = item.isSender
                leftItem.text = item.content
                leftItem.avatarString = item.userHeader
                leftItem.name = item.userName
                leftItem.sendTime = item.date
                leftItem.messageSendState = CLChatMessageSendState(rawValue: item.status) ?? .sendSucess
                list.append(leftItem)
                break
            case .Image:
                let leftItem = CLChatImageItem()
                leftItem.messageId = item.messageId
                leftItem.isFromMyself = item.isSender
                leftItem.imagePath = item.imageURL
                leftItem.imageOriginalSize = CGSizeMake(CGFloat(item.width), CGFloat(item.height)) 
                leftItem.avatarString = item.userHeader
                leftItem.name = item.userName
                leftItem.sendTime = item.date
                leftItem.messageSendState = CLChatMessageSendState(rawValue: item.status) ?? .sendSucess
                list.append(leftItem)
                break
            case .Video:
                
                break
            case .Location:
                
                break
            case .Voice:
                let leftItem = CLChatVoiceItem()
                leftItem.messageId = item.messageId
                leftItem.isFromMyself = item.isSender
                leftItem.voiceUrl = item.voiceUrl
                leftItem.duration = TimeInterval(item.duration)
                leftItem.avatarString = item.userHeader
                leftItem.name = item.userName
                leftItem.sendTime = item.date
                leftItem.messageSendState = CLChatMessageSendState(rawValue: item.status) ?? .sendSucess
                list.append(leftItem)
                break
            case .File:
                
                break
            case .Command:
                
                break
            case nil:
                
                break
            }
            
        }
        return list
    }
    
    
    /// 插入聊天消息到数据库
    /// - Parameters:
    ///   - roomId: 房间id
    ///   - model: 聊天信息
    class func insertNewMessage(_ roomId: String,_ model: CLRowItemProtocol){
        let message = RSMessageContentModel()
        
        if let item = model as? CLChatTextItem {
            message.isSender = item.isFromMyself
            message.content = item.text
            message.userHeader = item.avatarString
            message.userName = item.name
            message.date = item.sendTime
            message.status = item.messageSendState.rawValue
            message.messageId = roomId + item.messageId
            message.type = MessageBodyType.Text.rawValue
        }else if let item = model as? CLChatImageItem {
            message.isSender = item.isFromMyself
            message.imageURL = item.imagePath ?? ""
            message.width = Float(item.imageOriginalSize.width)
            message.height = Float(item.imageOriginalSize.height)
            message.userHeader = item.avatarString
            message.userName = item.name
            message.date = item.sendTime
            message.messageId = roomId + item.messageId
            message.status = item.messageSendState.rawValue
            message.type = MessageBodyType.Image.rawValue
        }else if let item = model as? CLChatVoiceItem {
            message.isSender = item.isFromMyself
            message.duration = Float(item.duration)
            message.voiceUrl = item.voiceUrl
            message.userHeader = item.avatarString
            message.userName = item.name
            message.date = item.sendTime
            message.messageId = roomId + item.messageId
            message.status = item.messageSendState.rawValue
            message.type = MessageBodyType.Voice.rawValue
        }
        
        updataNewMessage(roomId, modelContent: message)
    }
}
